package com.qumitech.visualnovel.service.novel;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import lombok.extern.slf4j.Slf4j;
import java.util.*;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.qumitech.visualnovel.model.common.BaseResponse;
import com.qumitech.visualnovel.datasource.app.*;
import com.qumitech.visualnovel.datasource.console.*;
import com.qumitech.visualnovel.datasource.editor.BookDAOMapper;
import com.qumitech.visualnovel.datasource.editor.ChapterDAOMapper;
import com.qumitech.visualnovel.datasource.editor.AuthorDAOMapper;
import com.qumitech.visualnovel.model.dao.editor.BookDAO;
import com.qumitech.visualnovel.model.dao.editor.AuthorDAO;

@Slf4j
@Service
public class DashboardServiceImpl implements DashboardService {
    @Autowired
    BookDAOMapper bookDAOMapper;

    @Autowired
    ChapterDAOMapper chapterDAOMapper;

    @Autowired
    AuthorDAOMapper authorDAOMapper;

    @Autowired
    AccountRegisterStatisticsMapper accountRegisterStatisticsMapper;

    @Autowired
    AccountLoginStatisticsMapper accountLoginStatisticsMapper;

    @Autowired
    DailyRechargeDataDOMapper dailyRechargeDataDOMapper;

    @Autowired
    UserBookDOMapper userBookDOMapper;

    @Override
    public BaseResponse authorList(Integer offset, Integer limit) {
        int total = authorDAOMapper.dashboardTotal();
        List<AuthorDAO> list = authorDAOMapper.dashboardList(offset, limit);

        BaseResponse result = new BaseResponse(new JSONArray(), total);
        for (AuthorDAO item : list) {
            JSONObject jItem = new JSONObject();
            jItem.put("name", item.getName());
            jItem.put("pseudonym", item.getPseudonym());
            jItem.put("contractType", item.getContractType());
            jItem.put("country", item.getCountry());
            jItem.put("updatedAt", item.getUpdatedAt());
            result.getJSONArray("data").add(jItem);
        }
        return result;
    }

    @Override
    public BaseResponse bookList(Integer offset, Integer limit) {
        int total = bookDAOMapper.dashboardTotal(null, "In Review");
        List<BookDAO> list = bookDAOMapper.dashboardList(null, "In Review", offset, limit);

        BaseResponse result = new BaseResponse(new JSONArray(), total);
        for (BookDAO item : list) {
            JSONObject jItem = new JSONObject();
            jItem.put("title", item.getTitle());
            jItem.put("author", bookDAOMapper.getAuthorName(item.getId()));
            jItem.put("reviewState", item.getReviewState());
            jItem.put("chaptersNum", chapterDAOMapper.getReviewStateNum(item.getId(), "In Review"));
            jItem.put("updatedAt", item.getUpdatedAt());
            result.getJSONArray("data").add(jItem);
        }
        return result;
    }


    @Override
    public BaseResponse dashboardHome() {
        Integer iosRegNum = accountRegisterStatisticsMapper.getDashboardRegNum("ios");
        Integer androidRegNum = accountRegisterStatisticsMapper.getDashboardRegNum("android");
        Integer iosLoginNum = accountLoginStatisticsMapper.getDashboardLoginNum("ios");
        Integer androidLoginNum = accountLoginStatisticsMapper.getDashboardLoginNum("android");
        Float totalRecharge = dailyRechargeDataDOMapper.getDashboardRechargeNum();
        Integer chapterUnlockNum = userBookDOMapper.getDashboardUnlockNum("book_chapter_unlock", "id");
        Integer optionUnlockNum = userBookDOMapper.getDashboardUnlockNum("book_frame_unlock", "id");
        Integer chapterUnlockUidNum = userBookDOMapper.getDashboardUnlockNum("book_chapter_unlock", "uid");
        Integer optionUnlockUidNum = userBookDOMapper.getDashboardUnlockNum("book_frame_unlock", "uid");

        JSONObject result = new JSONObject();
        result.put("iosRegNum", iosRegNum);
        result.put("androidRegNum", androidRegNum);
        result.put("iosLoginNum", iosLoginNum);
        result.put("androidLoginNum", androidLoginNum);
        result.put("totalRecharge", totalRecharge);
        result.put("chapterUnlockNum", chapterUnlockNum);
        result.put("chapterUnlockUidNum", chapterUnlockUidNum);
        result.put("optionUnlockNum", optionUnlockNum);
        result.put("optionUnlockUidNum", optionUnlockUidNum);

        return new BaseResponse(result);
    }
}
